﻿using MediatR;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;

namespace MS.BasicData.Read.API.Applications.Roles
{
    public class ListRoleRequestHandler : IRequestHandler<ListRoleRequestModel, ApiRestult>
    {
        /// <summary>
        /// 角色仓储
        /// </summary>
        private readonly IRoleBaseRepository _roleRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleRepository"></param>
        public ListRoleRequestHandler(IRoleBaseRepository roleRepository)
        {
            _roleRepository = roleRepository;
        }

        public async Task<ApiRestult> Handle(ListRoleRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Message = "获取角色列表成功",
                Code = Restult.Success
            };

            var roleinfo = await _roleRepository.GetAllAsync(a => a.IsDeleted == false);
            if (!string.IsNullOrEmpty(request.RoleName))
            {
                roleinfo = roleinfo.Where(r => r.RoleName.Contains(request.RoleName)).ToList();
            }

            restult.Data = roleinfo;
            return restult;
        }
    }


    public class ListRoleRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 角色名称
        /// </summary>
        public string? RoleName { get; set; } 

    }

}
